package com.example.foodsy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.entity.CompanyInfo;
import com.example.foodsy.entity.FarmInfo;
import com.example.foodsy.entity.SlauInfo;
import com.example.foodsy.entity.WholInfo;
import com.example.foodsy.mapper.CompanyInfoMapper;
import com.example.foodsy.mapper.SlauInfoMapper;
import com.example.foodsy.mapper.WholInfoMapper;
import com.example.foodsy.service.WholInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 批发商产品批号信息表 服务实现类
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@Service
public class WholInfoServiceImpl extends ServiceImpl<WholInfoMapper, WholInfo> implements WholInfoService {

	@Autowired
	private SlauInfoMapper slauInfoMapper;

	@Autowired
	private CompanyInfoMapper companyInfoMapper;
	@Override
	public boolean addWholInfo(WholInfo wholInfo) {

		return save(wholInfo);
	}

	@Override
	public AddressDTO<SlauInfo> getSlauInfo(Integer sbId) {
		SlauInfo sbId1 = slauInfoMapper.selectOne(new QueryWrapper<SlauInfo>().eq("sb_id", sbId));

		CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", sbId1.getSlauId()));

		AddressDTO<SlauInfo> addressDTO = new AddressDTO<>();
		addressDTO.setOriginBatchId(sbId1.getBatchId());
		addressDTO.setCompanyName(companyInfo.getName());

		return addressDTO;
	}

	@Override
	public List<AddressDTO<WholInfo>> getWholInfoBystate(Integer state) {
		List<WholInfo> wholInfos = baseMapper.selectList(new QueryWrapper<WholInfo>().eq("state", state));
		List<AddressDTO<WholInfo>> addressDTOS = new ArrayList<>();

		for (WholInfo wholInfo : wholInfos) {
			AddressDTO<WholInfo> addressDTO = new AddressDTO<>();
			CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", wholInfo.getWholId()));
			addressDTO.setCompanyName(companyInfo.getName());
			addressDTO.setCompanyInfo(wholInfo);

			SlauInfo slauInfo = slauInfoMapper.selectOne(new QueryWrapper<SlauInfo>().eq("sb_id", wholInfo.getSbId()));
			addressDTO.setOriginBatchId(slauInfo.getBatchId());

			addressDTOS.add(addressDTO);
		}

		return addressDTOS;
	}


}
